<?php

use Yaf\Request_Abstract;

class UserModel
{
    const Cookie_File = "cookie";
    const User_Data = "data";


    private $id;

    private $username;

    private $password;

    private $savaDir;

    private $type;

    /**
     * UserModel constructor.
     * @param $arr
     */
    public function __construct($arr)
    {
        if (is_array($arr)) {
            //得到所有的成员属性
            $keys = array_keys(get_class_vars(__CLASS__));

            foreach ($arr as $key => $value) {
                if (in_array($key, $keys)) {
                    $this->$key = $value;
                }
            }
        }
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * @return mixed
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * @return mixed
     */
    public function getSavaDir()
    {
        return $this->savaDir;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @param mixed $username
     */
    public function setUsername($username)
    {
        $this->username = $username;
    }

    /**
     * @return mixed
     */
    public function getUsername()
    {
        return $this->username;
    }


    /**
     * @param mixed $password
     */
    public function setPassword($password)
    {
        $this->password = $password;
    }

    /**
     * @param mixed $savaDir
     */
    public function setSavaDir($savaDir)
    {
        $this->savaDir = $savaDir;
    }

    /**
     * @param mixed $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    public function getCookieFile()
    {
        return $this->getSavaDir() . self::Cookie_File . '/';
    }

    public function getDataFile()
    {
        return $this->getSavaDir() . self::User_Data . '/';
    }

    public function typeToString()
    {
        $conf = [
            0 => '最高管理员',
            1 => '投放账号',
            2 => '小说账号'
        ];
        return $conf[$this->getType()] ?? "非法账号，请立即枪毙!";
    }


    /**
     * 获取用户model
     * @param Request_Abstract $request
     * @return UserModel
     */
    public static function getUserModel(Request_Abstract $request)
    {
        $user = $request->getParam('userInfo');
        return new UserModel($user);
    }
}
